#include <iostream>
#include <exception>
#include <cstdlib>
#include "TestCase.h"
#include "Node.h"
#include "List.h"
#include "ListFactory.h"
/*******************************************************************
// if U want to simulate a running environment with a limited heap memory of 100 bytes,
// please withdraw these comments
static int MEM_SIZE = 100; //the default size is 100 bytes, U can change it bigger.

void* operator new(std::size_t size){
    std::cout<<"New["<<size<<"]"<<std::endl;
    MEM_SIZE -= size;
    if(MEM_SIZE<0){
        std::cout<<"Not Enough Size of ["<<size<<"]"<<std::endl;
        MEM_SIZE += size;
        std::bad_alloc e;
        throw e;
    }
    return malloc(size);
}

void operator delete[](void *ptr){
    std::cout<<"DEL[]"<<std::endl;
    free(ptr);
}

void operator delete(void* ptr){
    std::cout<<"DEL"<<std::endl;
    free(ptr);
}
********************************************************************/
void TestList(ListType list_type){
    List<Node>* list  =  ListFactory<Node>::GetListInstance(list_type);
    Node n;
    for(int i =0; i < 20; i ++){
        n.value = i;
        list->Append(n);
    }

    n.value = 20;
    list->Append(n);
    list->Delete(15);
    n.value = 30;
    list->Insert(3,n);
    list->DumpList(std::cout);
    std::cout<<"After clear the list:"<<std::endl;
    list->Clear();
    list->DumpList(std::cout);
    n.value = 4;
    std::cout<<"Insert(0,n):"<<list->Insert(0,n)<<std::endl;
    std::cout<<"Append(n):"<<list->Append(n)<<std::endl;
    std::cout<<"Delete(0):"<<list->Delete(0)<<std::endl;
    list->DumpList(std::cout);
    delete list;
}

